package distance;

import org.junit.Test;

import java.awt.*;
import java.awt.geom.Point2D;
import java.util.Scanner;

public class DistanceTest
{
    private static final double EARTH_RADIUS = 6371393; // 平均半径,单位：m

    /**
     * 通过AB点经纬度获取距离
     * @param pointA A点(经，纬)
     * @param pointB B点(经，纬)
     * @return 距离(单位：米)
     */
    public static double getDistance(Point2D pointA, Point2D pointB) {
        // 经纬度（角度）转弧度。弧度用作参数，以调用Math.cos和Math.sin
        double radiansAX = Math.toRadians(pointA.getX()); // A经弧度
        double radiansAY = Math.toRadians(pointA.getY()); // A纬弧度
        double radiansBX = Math.toRadians(pointB.getX()); // B经弧度
        double radiansBY = Math.toRadians(pointB.getY()); // B纬弧度

        // 公式中“cosβ1cosβ2cos（α1-α2）+sinβ1sinβ2”的部分，得到∠AOB的cos值
        double cos = Math.cos(radiansAY) * Math.cos(radiansBY) * Math.cos(radiansAX - radiansBX)
                + Math.sin(radiansAY) * Math.sin(radiansBY);
//        System.out.println("cos = " + cos); // 值域[-1,1]
        double acos = Math.acos(cos); // 反余弦值
//        System.out.println("acos = " + acos); // 值域[0,π]
//        System.out.println("∠AOB = " + Math.toDegrees(acos)); // 球心角 值域[0,180]
        return EARTH_RADIUS * acos; // 最终结果
    }

    private  double getAngle1(double lat_a, double lng_a, double lat_b, double lng_b) {

        double y = Math.sin(lng_b-lng_a) * Math.cos(lat_b);
        double x = Math.cos(lat_a)*Math.sin(lat_b) - Math.sin(lat_a)*Math.cos(lat_b)*Math.cos(lng_b-lng_a);
        double brng = Math.atan2(y, x);

        brng = Math.toDegrees(brng);
        if(brng < 0) {
            brng = brng +360;
        }
        return brng;

    }

    @Test
    public void test()
    {
        // 北京 东单地铁站
        Point2D pointDD = new Point2D.Double(116.425249, 39.914504);
        // 北京 西单地铁站
        Point2D pointXD = new Point2D.Double(116.382001, 39.913329);

        System.out.println(pointDD.toString());
        System.out.println(getDistance(pointDD,pointXD));
        System.out.println(getAngle1(0,0,23,55));
    }

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        Point point1 = new Point();
        Point point2 = new Point();
        double x, y;
        point1.setLocation(5.0, 5.0);
        point2.setLocation(1.0, 1.0);
        System.out.println("请分别输入点x,y的坐标：");
        x = scan.nextDouble();
        y = scan.nextDouble();
        boolean pdline = (x - point1.getX()) * (point1.getY() - point2.getY()) == (point1.getX() - point2.getX())
                * (y - point1.getY());
        if (pdline) {
            System.out.println("您输入的点在该条直线上");
        } else {
            System.out.println("您输入的点不在该条直线上");
        }
        scan.close();
    }


}
